<?php
/**
 * Created by PhpStorm.
 * User: 17274
 * Date: 2018/3/27
 * Time: 21:06
 */
namespace app\admin\model;

use think\Model;
use think\Validate;
use think\Config;

class RuleModel extends  Model
{
    protected $table = 'yui_rule';
    /*
     * rule
     * 验证data
     * @params $data
     * 新增和修改时验证
     * */
    public function validate_model(){
        $rule = [
            'role_id' => 'token'
        ];
        $msg = [
        ];
        $validate = new Validate($rule, $msg);
        $result = $validate->check(input('post.'));
        if(!$result){
            return [
                'code'=>0,
                'text'=> $validate->getError()
            ];
        }
        return ['code'=>1];
    }

    /*
    * 根据RoleId删除资源权限表
    * */
    public function auth(){
        $role_id=input("post.role_id/s");
        $arr=input("post.arr/a");
        $data=[];
        foreach($arr as $value){
            $value=json_decode(htmlspecialchars_decode($value));
            $data[]=array(
                "controller"=>$value->controller,
                "action"=>$value->action,
                "role_id"=>$role_id,
                "created_at"=>time(),
                "updated_at"=>time(),
            );

        }unset($value);
        //事务开始
        $this->startTrans();
        $count=$this->where("role_id='$role_id'")->count();
        $count2=count($arr);
        //先删除role_id相关的所有rule
        if($this->where("role_id='$role_id'")->delete() == $count){
            $i=0;
            foreach($data as $value){
                if($this->insert($value)){
                    $i++;
                }
            }unset($value);
            if($i==$count2){
                $this->commit();
                if(!empty($_SESSION["username"])){
                    $s_user_id=$_SESSION["user_id"];
                    $s_role_id=$_SESSION["role_id"];
                    $s_username=$_SESSION["username"];
                    $s_name=$_SESSION["name"];
                    session_unset();
                    $role_rule=$this->getByRoleId($s_role_id);
                    foreach($role_rule as $value){
                        if(empty($_SESSION["authority"][$value["controller"]])){
                            $_SESSION["authority"][$value["controller"]]=[];
                        }
                        array_push($_SESSION["authority"][$value["controller"]],$value["action"]);
                    }unset($value);
                    $_SESSION["user_id"]=$s_user_id;
                    $_SESSION["role_id"]=$s_role_id;
                    $_SESSION["username"]=$s_username;
                    $_SESSION["name"]=$s_name;
                }
                $params=[
                    Config::get('op_type')[3],
                    Config::get('op_module')[1],
                    Config::get('op_text')[2],
                    (new RoleModel())->getByMaps(['role_id'=>$role_id])->toArray()['name']
                ];
                tag('logs_db',$params);
                return op_success();
            }
            $this->rollback();
            return op_fail();
        }
        $this->rollback();
        return op_fail();
    }

    /*
    * 根据RoleId获取资源权限表
    * */
    public function getByRoleId($role_id){
        return $this->where(["role_id"=>$role_id])->select();
    }

}